package com.service.user.impl;

import com.bean.Js;
import com.bean.Jsqx;
import com.bean.Xtcd;
import com.bean.Yhjs;
import com.common.Cache;
import com.dao.JsMapper;
import com.dao.JsqxMapper;
import com.dao.XtcdMapper;
import com.dao.YhjsMapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.service.core.AbstractService;
import com.service.user.JsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * Created by lvjianqing on 2017/9/10.
 */
@Service
public class JsServiceImpl extends AbstractService<Js, Long> implements JsService {
    @Autowired
    private JsMapper jsMapper;
    @Autowired
    private XtcdMapper xtcdMapper;
    @Autowired
    private JsqxMapper jsqxMapper;
    @Autowired
    private YhjsMapper yhjsMapper;

    @Override
    public Page findByPage(int pageNum, int pageSize, HashMap<String, Object> paraMap) {
        PageHelper.offsetPage(pageNum,pageSize);
        Page page = (Page) jsMapper.findAll(paraMap);
        return page;
    }

    @Override
    public Js findById(Long id) {
        return jsMapper.selectByPrimaryKey(id);
    }
    @Override
    public List<Long> findJsIdByYhId(Integer yhid) {
        List<Long> res = new ArrayList<>();
        if (yhid==null) {
            return res;
        }
        List<Long> list = yhjsMapper.findJsidByYhid(yhid);
        if (list == null || list.size() <= 0) {
            return res;
        }
        return list;
    }
    @Override
    public void saveJs(Js js) {
        if(js!=null){
            if(js.getId()!=null){
                jsMapper.updateByPrimaryKeySelective(js);
            }else{
                js.setSjid(1l);
                jsMapper.insertSelective(js);
            }
            Cache.initJsMap();
        }
    }

    @Override
    public void deleteById(Long id) {
        jsMapper.deleteByPrimaryKey(id);
    }

    @Override
    public Page findXtcdByPage(int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        Page page = (Page) xtcdMapper.findAll();
        return page;
    }

    @Override
    public List<Long> findByJsId(Long jsid) {
        if (jsid != null) return jsqxMapper.findByJsId(jsid);
        return new ArrayList<>();
    }

    @Override
    public void saveJsqx(Long jsid, Long[] ids) {
        if (jsid != null) {
            jsqxMapper.deleteByJsId(jsid);
            HashSet<Long> sjcds = new HashSet<>();
            for (Long id : ids) {
                if (id <= 0) {
                    break;
                }
                Jsqx jsqx = new Jsqx();
                jsqx.setJsid(jsid);
                jsqx.setXtcdid(id);
                int res = jsqxMapper.insertSelective(jsqx);
                Xtcd sjcd = xtcdMapper.findSjCdById(id);
                if(sjcd!=null){
                    sjcds.add(sjcd.getId());
                }
            }
            Iterator<Long> it = sjcds.iterator();
            while (it.hasNext()) {
                Long id = it.next();
                Jsqx jsqx = new Jsqx();
                jsqx.setJsid(jsid);
                jsqx.setXtcdid(id);
                int res = jsqxMapper.insertSelective(jsqx);
            }
        }
    }

    @Override
    public void saveYhjs(Integer yhid, Long[] ids) {
        if (yhid!=null) {
            yhjsMapper.deleteByYhid(yhid);
            for (Long id : ids) {
                if (id <= 0) {
                    break;
                }
                Yhjs yhjs = new Yhjs();
                yhjs.setJsid(id);
                yhjs.setYhid(yhid);
                int res = yhjsMapper.insertSelective(yhjs);
            }
        }
    }
}
